<?xml version="1.0" encoding="utf-8"?>
<Content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:blueonionsoftware-com:bloget:runtime:data">
  <text>In 6.0, when we moved to .NET 2.0, we left out all the boundary code using old style syntax for CLR.  Visual Studio 2005 allowed us to set the project settings to use â€œOld Style Syntaxâ€? for CLR. I noticed that some of our new code started using the new syntax. 

As part of the decoupling work, I wrote some generic reporting xml object converters that will convert from Reporting Xml Objects to our tagged attributes. Since some of our code used old style syntax for CLR and some code used new syntax, I didnâ€™t have much of a choice but to convert all of our boundary code to use new CLR syntax. 

Going forward, if we have to write the boundary code from unmanaged to managed side, please set your project setting to use â€œusing  CLRâ€? instead of â€œUsing CLR with old style syntaxâ€?.

Here is the msdn link that explains the migration from old style to new style (Managed Extensions) :

http://msdn2.microsoft.com/en-us/library/aa712574(vs.71).aspx

Some important notes regarding the managed extensions :

Managed C++ classes cannot have a â€œDisposeâ€? method. This is a reserved word. To implement IDisposable, implement the destructor for Managed C++ Class like in regular C++. In Managed C++, the destructor is te the DISPOSE method. To dispose a managed object, you would call â€œdelete &lt;reference to the objectâ€? instead of â€œ&lt;reference to the object&gt;.disposeâ€?. Syntax for the Finalizers in managed C++ class is â€œClassName::!ClassNameâ€?. 

When you have managed objects within a native class, we have to use either &lt;gc_root&gt; or &lt;auto_gcroot&gt; template. &lt;/auto_gcroot&gt;&lt;auto_gcroot&gt; acts like a smart pointer making sure the dispose gets called when all the references to it are released. 
 
&lt;pre&gt;
               class NativeClassA
                {
                   msclr::gcroot&lt;streamwriter ^&gt; m_sw;
                    public:
                   NativeClassA ()
                    {
                       m_sw = File::CreateText("d:\\temp.txt");
                   }
                   ~ NativeClassA ()
                   {
                       delete m_sw;
                   }
               };
 &lt;/streamwriter&gt;&lt;/pre&gt;
 
Notice that with gcroot template, you have to manually class â€œdeleteâ€? for the stream writer which disposes the stream writer. 

&lt;pre&gt;
               class NativeClassB
                {
                   msclr::auto_gcroot&lt;streamwriter ^&gt; m_sw;
                    public:
                   NativeClassB ()
                    {
                       m_sw = File::CreateText("d:\\temp.txt");
                   }
                   ~ NativeClassB ()
                   {
                   }
               };
 &lt;/streamwriter&gt;&lt;/pre&gt;

Now, the destructor does not have to delete the stream writer since it will automatically get disposed when the objects of NativeClassB are deleted.
 

 

Please let me know, if you have any questions.

 

Thanks

Sunanda

&lt;/auto_gcroot&gt;&lt;/gc_root&gt;&lt;/reference&gt;</text>
  <last_update>2007-10-04T00:52:33.0910806Z</last_update>
  <source_title />
  <source_link />
  <comments />
  <references />
  <attachments />
</Content>